    package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.XML

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat
//导入grooby的json解析api包

/**
     *
     * @author luper
     *
     */

    public class GwZyyOrder implements ServiceBackInvoke{
        public static final Log log = LogFactory.getLog(GwZyyOrder.class);
        private String charset = "gbk"; //设置字符集
        private int timeout = 100000; //设置请求超时时间
        private int enableSSLflag = 1;
        private String casecharset = "utf-8";
        private int caseenableSSLflag = 1;

        public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response){
            log.debug("-----GwZyyOrder Begin Systeam----------");
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            String requestJSONStr =Util.getRequestBody(request, "UTF-8");
            log.debug("requestJSONStr:" + requestJSONStr);

            request.getParameter("interfacetype");//获取接口请求类型：create 承保理赔异议信息工单新建/search 保理赔异议信息工单查询
            String v_interfacetype=request.getParameter("interfacetype");
            log.debug("v_interfacetype:"+v_interfacetype);
            eliteBean.update("insert into INTERFACE_JSONBODY values(sysdate,'8099', 'GwZyyOrder','"+requestJSONStr.toString()+"',sysdate,null)","dataSource");
			
			     
			try{
            //承保理赔异议信息工单新建
             if(v_interfacetype=='create'){
			 
				
				
                JSONObject json  = new JSONObject().fromObject(requestJSONStr);
				
				String customername = "";
				if(json.has("customername")&&json.getString("customername").length()>0){
					customername = json.getString("customername");               
				}else{
					JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "客户姓名不能为空！");
                    return jsonALL3;
				}

				String mobile = "";
				if(json.has("mobile")&&json.getString("mobile").length()>0){
					mobile = json.getString("mobile");               
				}else{
					JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "客户电话不能为空！");
                    return jsonALL3;
				}
				
				String businesstype = "";
				if(json.has("businesstype")&&json.getString("businesstype").length()>0){
					businesstype = json.getString("businesstype");               
				}else{
					JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "业务类型不能为空！");
                    return jsonALL3;
				}
				String businessno = "";
				if(json.has("businessno")&&json.getString("businessno").length()>0){
					businessno = json.getString("businessno");               
				}else{
					JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "业务号不能为空！");
                    return jsonALL3;
				}
				String content = "";
				if(json.has("content")&&json.getString("content").length()>0){
					content = json.getString("content");               
				}else{
					JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "异议信息不能为空！");
                    return jsonALL3;
				}
				String gender ="";
				if(json.has("gender")){//性别
					gender = json.getString("gender");        			
				}
				String email ="";
				if(json.has("email")){//性别
					email = json.getString("email");        			
				}
				
				
                Map policyMap=new HashMap();
                Map caseMap=new HashMap();
                if(businesstype == 'P'){
                    //调用PolicyContent()方法，获取保单信息
                   policyMap=PolicyContent(businessno,eliteBean);
                    //log.debug("policyMap主方法中输出-------------------------------"+policyMap);
                    if(policyMap.get("errorcode")=="-1"){
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "-1");
                        jsonALL3.put("retMessage","未获取到保单信息");
                        return jsonALL3;
                    }
                }else if(businesstype == 'L'){
                    //调用CaseContent()方法，获取案件信息
                    caseMap=CaseContent(businessno,eliteBean);
                    if(caseMap.get("errorcode")=="-1"){
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "-1");
                        jsonALL3.put("retMessage", "未获取到案件信息");
                        return jsonALL3;
                    }
                }else{
                    JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "业务类型参数不正确！");
                    return jsonALL3;
                }

                //生成objective_guid  工单号
                def querysql11="select sysguid objective_guid,'CBYY'||to_char(sysdate,'YYYYMMDDhh24mi')||(case when SEQ_GWZTSORDER.nextval>=1 and SEQ_GWZTSORDER.nextval<10 then '000'||SEQ_GWZTSORDER.nextval  when SEQ_GWZTSORDER.nextval>=10 and SEQ_GWZTSORDER.nextval<100  then '00'||SEQ_GWZTSORDER.nextval  when SEQ_GWZTSORDER.nextval>=100 and SEQ_GWZTSORDER.nextval<1000  then '0'||SEQ_GWZTSORDER.nextval else to_char(SEQ_GWZTSORDER.nextval)end) workorderid from dual";
                def list11=eliteBean.chaxun_sql(querysql11,"dataSource");
                log.debug("list11-------------------------------"+list11);
                def map11=list11[0];
                log.debug("map11-------------------------------"+map11);
                def v_objective_guid=map11.get("objective_guid");
                def v_workorderid=map11.get("workorderid");
                log.debug("v_objective_guid-------------------------------"+v_objective_guid);
                log.debug("v_workorderid-------------------------------"+v_workorderid);

                //新老客户检查
                def querysql1="select count(*) count1 from customer where customername='"+customername+"' and mobile='"+mobile+"'";
                def list1=eliteBean.chaxun_sql(querysql1,"dataSource");
                log.debug("list1-------------------------------"+list1);

                for(def k=0;k<list1.size();k++){
                    def map1=list1[k];
                    def v_count1=map1.get("count1");
                    log.debug("v_count1---v_count1-----v_count1----"+v_count1);

                    //新客户新建承保理赔异议信息工单
                    if(v_count1==0){
                        //新customer_guid
                        def querysql2="select sysguid new_customer_guid from dual";
                        def list2=eliteBean.chaxun_sql(querysql2,"dataSource");
                        log.debug("list2-------------------------------"+list2);
                        def map2=list2[0];
                        log.debug("map2-------------------------------"+map2);
                        def v_customer_guid=map2.get("new_customer_guid");
                        log.debug("v_customer_guid-------------------------------"+v_customer_guid);

                        //生成 customer
                        def updatesql3="insert into customer(CUSTOMER_GUID,customertype_id,CUSTOMERNAME,MOBILE,WORKSTATUS)select '"+v_customer_guid+"','YYjoin','"+customername+"','"+mobile+"','ONLINE' FROM dual";
                        def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                        log.debug("updatesqlresult3:"+updatesqlresult3);

                        //生成 objective
                        if(businesstype == 'P') {
                            def updatesql4 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,POLICYNO,CARNO,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,frameno,engineno,coverage_name,brandname,carage,seatnum,renewinsuranceflag,isnwemployee,insured,organizaname,organiza,realvisiter_flag,callstarttime,b_plate,business_source,procuration)values('" + v_objective_guid + "','XKI9F3','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','000146',sysdate,'6USURJ','WORDER',null,'6USURJ','OESLNM','0','" + customername + "','" + mobile + "','" + businessno + "','" + policyMap.get("carno") + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,'" + policyMap.get("frameno") + "','" + policyMap.get("engineno") + "','" + policyMap.get("coverage_name") + "','" + policyMap.get("brandname") + "','" + policyMap.get("carage") + "','" + policyMap.get("seatnum") + "','" + policyMap.get("renewinsuranceflag") + "','" + policyMap.get("isnwemployee") + "','" + policyMap.get("insured") + "','" + policyMap.get("organizaname") + "','" + policyMap.get("organiza") + "',0,sysdate,'"+policyMap.get("b_plate")+"','"+policyMap.get("business_source")+"','"+policyMap.get("procuration")+"')";
                            def updatesqlresult4 = eliteBean.update(updatesql4, "dataSource");
                            log.debug("updatesqlresult4:" + updatesqlresult4);
                            //生成 tabc
                            def updatesql6="insert into tabcorder_cbyy(RELATION_ID,workroderid,ORDER_DESCRIPTION) values('"+v_objective_guid+"','"+v_workorderid+"','"+content+"')";
                            def updatesqlresult6=eliteBean.update(updatesql6,"dataSource");
                            log.debug("updatesqlresult6:"+updatesqlresult6);



                        }else if(businesstype == 'L'){
                            def updatesql4 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,realvisiter_flag,callstarttime,reportno)values('" + v_objective_guid + "','XKI9F3','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','000146',sysdate,'6USURJ','WORDER',null,'6USURJ','OESLNM','0','" + customername + "','" + mobile + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,0,sysdate,'"+businessno+"')";
                            def updatesqlresult4 = eliteBean.update(updatesql4, "dataSource");
                            log.debug("updatesqlresult4:" + updatesqlresult4);
                            //生成 tabc
                            def updatesql6="insert into tabcorder_cbyy(RELATION_ID,workroderid,ORDER_DESCRIPTION,report_pkey,report_nodecode,arrive_time,report_time,examinename,examine_type,repairfactoryname,losser,losser_list,loss_type) values('"+v_objective_guid+"','"+v_workorderid+"','"+content+"','"+businessno+"','"+caseMap.get("report_nodecode")+"','"+caseMap.get("arrive_time")+"','"+caseMap.get("report_time")+"','"+caseMap.get("examinename")+"','"+caseMap.get("examine_type")+"','"+caseMap.get("repairfactoryname")+"','"+caseMap.get("losser")+"','"+caseMap.get("losser_list")+"','"+caseMap.get("loss_type")+"')";
                            def updatesqlresult6=eliteBean.update(updatesql6,"dataSource");
                            log.debug("updatesqlresult6:"+updatesqlresult6);
                        }

                        //生成 elitetask
                        def updatesql5="insert into elitetask(ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,EXPECTSTARTTIME,EXPECTENDTIME,PRIORITY,HANDLEGROUP_ID,CUSTOMER_GUID,CURSTEPSTATUS,STEPFORECASTDATE,CURSTEP_ID_MAP, MAP_ID, OBJECTIVESUBSTATUS,OBFLAG,handlerole_id)values(sysguid,'"+v_objective_guid+"','XKI9F3',sysdate,sysdate,'NORMAL','ORG016','"+v_customer_guid+"','OPEN',sysdate,'OESLNM','6USURJ',null,'0',null)";
                        def updatesqlresult5=eliteBean.update(updatesql5,"dataSource");
                        log.debug("updatesqlresult5:"+updatesqlresult5);


                        //返回成功结果
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "1");
                        jsonALL3.put("retMessage", "success");
                        jsonALL3.put("Workorderid", v_workorderid);
                        return jsonALL3;
                    }

                    //老客户新建承保理赔异议信息工单
                    if(v_count1>0){
                        //查已有的customer_guid
                        def querysql7="select customer_guid customer_guid from customer where customername='"+customername+"' and mobile='"+mobile+"' and rownum=1";
                        def list7=eliteBean.chaxun_sql(querysql7,"dataSource");
                        log.debug("list7-------------------------------"+list7);
                        def map7=list7[0];
                        log.debug("map7-------------------------------"+map7);
                        def v_customer_guid=map7.get("customer_guid");
                        log.debug("v_customer_guid-------------------------------"+v_customer_guid);

                        if(businesstype == 'P') {
                            def updatesql8 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,POLICYNO,CARNO,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,frameno,engineno,coverage_name,brandname,carage,seatnum,renewinsuranceflag,isnwemployee,insured,organizaname,organiza,realvisiter_flag,callstarttime,b_plate,business_source,procuration)values('" + v_objective_guid + "','XKI9F3','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','000146',sysdate,'6USURJ','WORDER',null,'6USURJ','OESLNM','0','" + customername + "','" + mobile + "','" + businessno + "','" + policyMap.get("carno") + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,'" + policyMap.get("frameno") + "','" + policyMap.get("engineno") + "','" + policyMap.get("coverage_name") + "','" + policyMap.get("brandname") + "','" + policyMap.get("carage") + "','" + policyMap.get("seatnum") + "','" + policyMap.get("renewinsuranceflag") + "','" + policyMap.get("isnwemployee") + "','" + policyMap.get("insured") + "','" + policyMap.get("organizaname") + "','" + policyMap.get("organiza") + "',0,sysdate,'"+policyMap.get("b_plate")+"','"+policyMap.get("business_source")+"','"+policyMap.get("procuration")+"')";
                            def updatesqlresult8 = eliteBean.update(updatesql8, "dataSource");
                            log.debug("updatesqlresult4:" + updatesqlresult8);
                            //生成 tabc
                            def updatesql10="insert into tabcorder_cbyy(RELATION_ID,workroderid,ORDER_DESCRIPTION) values('"+v_objective_guid+"','"+v_workorderid+"','"+content+"')";
                            def updatesqlresult10=eliteBean.update(updatesql10,"dataSource");
                            log.debug("updatesqlresult10:"+updatesqlresult10);



                        }else if(businesstype == 'L'){
                            def updatesql8 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,realvisiter_flag,callstarttime,reportno)values('" + v_objective_guid + "','XKI9F3','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','000146',sysdate,'6USURJ','WORDER',null,'6USURJ','OESLNM','0','" + customername + "','" + mobile + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,0,sysdate,'"+businessno+"')";
                            def updatesqlresult8 = eliteBean.update(updatesql8, "dataSource");
                            log.debug("updatesqlresult8:" + updatesqlresult8);
                            //生成 tabc
                            def updatesql10="insert into tabcorder_cbyy(RELATION_ID,workroderid,ORDER_DESCRIPTION,report_pkey,report_nodecode,arrive_time,report_time,examinename,examine_type,repairfactoryname,losser,losser_list,loss_type) values('"+v_objective_guid+"','"+v_workorderid+"','"+content+"','"+businessno+"','"+caseMap.get("report_nodecode")+"','"+caseMap.get("arrive_time")+"','"+caseMap.get("report_time")+"','"+caseMap.get("examinename")+"','"+caseMap.get("examine_type")+"','"+caseMap.get("repairfactoryname")+"','"+caseMap.get("losser")+"','"+caseMap.get("losser_list")+"','"+caseMap.get("loss_type")+"')";
                            def updatesqlresult10=eliteBean.update(updatesql10,"dataSource");
                            log.debug("updatesqlresult10:"+updatesqlresult10);
                        }



                        //生成 elitetask
                        def updatesql9="insert into elitetask(ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,EXPECTSTARTTIME,EXPECTENDTIME,PRIORITY,HANDLEGROUP_ID,CUSTOMER_GUID,CURSTEPSTATUS,STEPFORECASTDATE,CURSTEP_ID_MAP, MAP_ID, OBJECTIVESUBSTATUS,OBFLAG,handlerole_id)values(sysguid,'"+v_objective_guid+"','XKI9F3',sysdate,sysdate,'NORMAL','ORG016','"+v_customer_guid+"','OPEN',sysdate,'OESLNM','6USURJ',null,'0',null)";
                        def updatesqlresult9=eliteBean.update(updatesql9,"dataSource");
                        log.debug("updatesqlresult9:"+updatesqlresult9);

                        //返回成功结果
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "1");
                        jsonALL3.put("retMessage", "success");
                        jsonALL3.put("Workorderid", v_workorderid);
                        return jsonALL3;
                    }

                    //返回失败结果
                    else{
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "-1");
                        jsonALL3.put("retMessage", "failure");
                        return jsonALL3;
                    }
                } 
            }
			}catch(Exception e){
				JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "-1");
                        jsonALL3.put("retMessage", e.getMessage());
                        return jsonALL3;
			}
            //承保理赔异议信息工单查询
            if(v_interfacetype=='search'){
                def SQL1="";
                JSONObject json = new JSONObject().fromObject(requestJSONStr);
                String businessno = json.getString("businessno");       	 //业务号
                log.debug("businessno:"+businessno);
                if(businessno == null || businessno=='') {
                    JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "业务号不能为空！");
                    return jsonALL3;
                }

                if(businessno !='' && businessno != null){
                    SQL1 ="and (policyno='"+businessno+"' or reportno='"+businessno+"')";
                }
                log.debug("SQL1:"+SQL1);

                def querysql20="select count(*) count2 from objective where MAP_ID='6USURJ' "+SQL1;
                def list20=eliteBean.chaxun_sql(querysql20,"dataSource");
                log.debug("list20-------------------------------"+list20);
                def map20=list20[0];
                log.debug("map20-------------------------------"+map20);
                def v_count2=map20.get("count2");
                log.debug("v_count2-------------------------------"+v_count2);

                if(v_count2>0){
                    def querysq2l="SELECT toc.order_description,c.COMMENTS v_dealresult FROM objective o left join (select WORKORDER_GUID ,COMMENTS  from (SELECT WORKORDER_GUID ,A.COMMENTS ,ROW_NUMBER() OVER(partition by A.WORKORDER_GUID order by A.MODIFIEDDATE desc nulls last) rn FROM workorderstep3 A left join objective o on o.objective_guid=A.workorder_guid where o.MAP_ID='6USURJ' and (policyno='"+businessno+"' or reportno='"+businessno+"') and A.WORKORDERSTEPSTATUS='END' ) B  WHERE b.RN = 1)c on c.WORKORDER_GUID=o.objective_guid left join tabcorder_cbyy toc on toc.relation_id=o.objective_guid   where  MAP_ID='6USURJ' "+SQL1;
                    List<?> l =eliteBean.chaxun_sql(querysq2l,"dataSource");
                    log.debug("l:"+l);
                    int count=l.size();
                    if(l.size()>=1){
                        JSONObject ja = new JSONObject();
                        JSONObject jb = new JSONObject();
                        JSONArray jc = new JSONArray();
                        ja.put("retCode", "1");
                        ja.put("retMessage", "success");

                        for(int i = 0;i<l.size();i++){
                            jb.clear();
                            Map<?, ?> m = (Map<?, ?>) l.get(i);
                            log.debug("Query:"+m.toString());
                            jb.put("content", m.get("order_description")  ==null?"":(String)m.get("order_description"));
                            jb.put("reply", m.get("v_dealresult")  ==null?"":(String)m.get("v_dealresult"));
                            jc.add(jb);
                        }
                        ja.put("Body", jc);
                        return ja;
                    }
                }else{
                    JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "未查询到相匹配的工单");
                    return jsonALL3;
                }
            }
			

        }

        //调用保单信息接口的方法
        public  Map PolicyContent(String policyno,EliteBean eliteBean){
            HashMap<String,Object> policyMap=new HashMap<String,Object>();
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

            String pwd='DHIC@CC#'+formatter.format(calendar.getTime()).substring(0,13).replace('-','').replace('-','').replace(' ','');  //加密接口密码
            log.debug("pwd1:"+pwd);
            pwd=Util.getMD5Str(pwd).toUpperCase();
            log.debug("pwd2:"+pwd);

            def poli_sub=policyno.substring(11,15);
            def polenlist=eliteBean.chaxun_sql("select count(parameter_id) nums from custom_parameter where USERFLAG=1  and parameter_type='non_insurance' and parameter_id='"+poli_sub+"'","dataSource");
            def polenmap=polenlist[0];
            int polen=polenmap.get("nums");
            def urllist=eliteBean.chaxun_sql("select url from sysc_interface where urltype='policyList'","dataSource");
            def urlmap=urllist[0];
            String url=urlmap.get("url");
            //url='http://10.252.1.3:9003/apiweb/CallCenter';
			 log.debug("polen:"+polen);
            log.debug("url:"+url);
            String Xmlbody;
			org.json.JSONObject policyJson;
            if(polen>0){//车险
                policyMap.put("workorderrisktype","1");

                Xmlbody= "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n" +
                        "<PACKET> \r\n" +
                        "  <HEAD> \r\n" +
                        "    <SystemCode>10001</SystemCode>  \r\n" +
                        "    <UserCode>CallCenter</UserCode> \r\n" +
                        "    <Password>"+pwd+"</Password> \r\n" +
                        "  </HEAD>  \r\n" +
                        "  <BODY>\r\n" +
                        "    <INPUTDATA>\r\n" +
                        "     <PolicyNo>"+policyno+"</PolicyNo>\r\n" +
                        "     <AppliName></AppliName>\r\n" +
                        "     <InsuredName></InsuredName>\r\n" +
                        "     <LicenseNo></LicenseNo>\r\n" +
                        "     <FrameNo/> \r\n" +
                        "     <EngineNo/>\r\n" +
                        "    </INPUTDATA>\r\n" +
                        "  </BODY> \r\n" +
                        "</PACKET>";
				
				Map headMap=new HashMap(); //设置头文件
				headMap.put("Content-Type", "text/xml; charset=gbk");
				def httpresult=Util.doHttpPostUseHead(url,Xmlbody,charset,timeout,enableSSLflag,headMap);
				log.debug("httpresult:"+httpresult);
				log.debug("Xmlbody:"+Xmlbody);
				policyJson=XML.toJSONObject(httpresult);
            }else{//非车险
                policyMap.put("workorderrisktype","2");
                /* Xmlbody= "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n" +
                        "<PACKET> \r\n" +
                        "  <HEAD> \r\n" +
                        "    <SystemCode>10012</SystemCode>  \r\n" +
                        "    <UserCode>CallCenter</UserCode> \r\n" +
                        "    <Password>"+pwd+"</Password> \r\n" +
                        "  </HEAD>  \r\n" +
                        "  <BODY>\r\n" +
                        "    <INPUTDATA>\r\n" +
                        "     <PolicyNo>"+policyno+"</PolicyNo>\r\n" +
                        "     <AppliName></AppliName>\r\n" +
                        "     <InsuredName></InsuredName>\r\n" +
                        "    </INPUTDATA>\r\n" +
                        "  </BODY> \r\n" +
                        "</PACKET>"; */
				//新非车接口
				def urllistNo=eliteBean.chaxun_sql("select url from sysc_interface where urltype='policyNoCar'","dataSource");
				def urlmapNo=urllistNo[0];
				String urlNo=urlmapNo.get("url");
				
				String request = "{\"policyNo\":\""+policyno+"\",\"appliName\":\"\",\"insuredName\":\"\",\"identifyNumber\":\"\"}";
				log.debug("url:"+urlNo);
				log.debug("request:"+request);
				Map headMap=new HashMap(); //设置头文件
				headMap.put("Content-Type", "application/json;charset=utf-8");
				def httpresult=Util.doHttpPostUseHead(urlNo,request,"UTF-8",timeout,enableSSLflag,headMap);
				log.debug("httpresult:"+httpresult);
				policyJson = new JSONObject().fromObject(httpresult);
				log.debug("policyJson:"+policyJson);
            }
            

            
			
			if(polen>0){
				if(policyJson.getJSONObject("PACKET").getJSONObject("HEAD").get("ResponseType") !=1){
					policyMap.put("errorcode","-1");
					policyMap.put("errormsg","未返回保单信息");
					return policyMap;
				}else{
					
					org.json.JSONObject policyList=policyJson.getJSONObject("PACKET").getJSONObject("BODY").getJSONObject("OUTDATE").getJSONObject("PolicyList").getJSONObject("Policy");
					log.debug("policyList:"+policyList);


					if(!policyList.has("LicenseNo")){
						policyMap.put("carno","000000");//车牌号
					}else{
						policyMap.put("carno",policyList.get("LicenseNo"));//车牌号
					}

					policyMap.put("insured",policyList.get("InsuredName"));//被保人
					def poli;
					log.debug("poli1:"+policyList.get("PolicyNo").toString().substring(1,3));
					if(policyList.get("PolicyNo").toString().substring(1,3)=="00"){
						poli=policyList.get("PolicyNo").toString().substring(1,3)+"020000";
					}else{
						poli=policyList.get("PolicyNo").toString().substring(1,3)+"000000";
					}
					log.debug("poli:"+poli);
					def rolegrouplist=eliteBean.chaxun_sql("select ROLEGROUPDESCRIPTION,ORG_ID from rolegroup where org_id='"+poli+"'","dataSource");
					def rolegroupmap=rolegrouplist[0];

					policyMap.put("organizaname",rolegroupmap.get("ROLEGROUPDESCRIPTION"));
					policyMap.put("organiza",rolegroupmap.get("ORG_ID"))

					if(policyList.has("BusinessPlate")){
						policyMap.put("b_plate",policyList.get("BusinessPlate"));//业务板块
					}else{
						policyMap.put("b_plate","");//业务板块
					}

					if(policyList.has("BusinessNature")){
						policyMap.put("business_source",policyList.get("BusinessNature"));//业务来源
					}else{
						policyMap.put("business_source","");//业务板块
					}
					if(policyList.has("AgentCode")){
						policyMap.put("procuration",policyList.get("AgentCode"));//代理人/经纪人
					}else{
						policyMap.put("procuration","");
					}
					if(policyList.has("FrameNo")){
						policyMap.put("frameno",policyList.get("FrameNo"));//车架号
					}else{
						policyMap.put("frameno","");//车架号
					}

					if(policyList.has("EngineNo")){
						policyMap.put("engineno",policyList.get("EngineNo"));//发动机号
					}else{
						policyMap.put("engineno","");//发动机号
					}

					if(policyList.has("BrandName")){
						policyMap.put("brandname",policyList.get("BrandName"));//车型
					}else{
						policyMap.put("brandname","");//车型
					}

					if(policyList.has("InsuranceType")){
						policyMap.put("coverage_name",policyList.get("InsuranceType"));//险种
					}else{
						policyMap.put("coverage_name","");//险种
					}

					if(policyList.has("Enrolldate")){
						policyMap.put("carage",policyList.getJSONObject("Enrolldate").get("content").toString().substring(0,4)+"-"+policyList.getJSONObject("Enrolldate").get("content").toString().substring(4,6)+"-"+policyList.getJSONObject("Enrolldate").get("content").toString().substring(6,8));//车辆初登日期
					}else{
						policyMap.put("carage","");
					}



					if(policyList.has("Seatcountshow")){
						policyMap.put("seatnum",policyList.get("Seatcountshow"));//座位数
					}else{
						policyMap.put("seatnum","0");//座位数
					}

					if(policyList.has("RenewInsuranceFlag")){
						policyMap.put("renewinsuranceflag",policyList.get("RenewInsuranceFlag"));//续保标识
					}else{
						policyMap.put("renewinsuranceflag","0");//续保标识
					}
					if(policyList.has("IsNwEmployee")){
						policyMap.put("isnwemployee",policyList.get("IsNwEmployee"));//核心V客户
					}else{
						policyMap.put("isnwemployee","");
					}

					if(policyList.has("InsuranceType")){
						policyMap.put("coverage_name",policyList.get("InsuranceType"));//险种名称
					}else{
						policyMap.put("coverage_name","");
					}
					/* if(policyList.has("RiskType")&&policyList.getString("RiskType")>0){
						org.json.JSONArray codes=policyList.getJSONArray("RiskType");
						org.json.JSONObject code=codes.getJSONObject(0);

					}else{

					} */
					//policyMap.put("riskcode",code.get("KindCode"));//险种名称
					policyMap.put("errorcode","1");
					policyMap.put("errormsg","成功");
					return policyMap;
				
				} 
			
			}else{
				if(polen<=0&&policyJson.get("code")!="200"){
					policyMap.put("errorcode","-1");
					policyMap.put("errormsg","未返回保单信息");
					return policyMap;
				}else{
					org.json.JSONArray policyListArray = policyJson.getJSONArray("data");
					if(policyListArray==null||policyListArray.length()<=0){
						policyMap.put("errorcode","-1");
						policyMap.put("errormsg","未返回保单信息");
						return policyMap;
					}
					org.json.JSONObject  policyList =  policyListArray.getJSONObject(0);
					org.json.JSONObject mainVo = policyList.getJSONObject("mainVo");
					if(mainVo.has("insuredName")){
						policyMap.put("insured",mainVo.get("insuredName"));//被保人
					}else{
						policyMap.put("insured","");
					}
					def poli;
					log.debug("poli1:"+policyList.get("operatorComCode").toString().substring(0,2));
					if(policyList.get("operatorComCode").toString().substring(0,2)=="00"){
						poli=policyList.get("operatorComCode").toString().substring(0,2)+"020000";
					}else{
						poli=policyList.get("operatorComCode").toString().substring(0,2)+"000000";
					}
					log.debug("poli:"+poli);
					def rolegrouplist=eliteBean.chaxun_sql("select ROLEGROUPDESCRIPTION,ORG_ID from rolegroup where org_id='"+poli+"'","dataSource");
					def rolegroupmap=rolegrouplist[0];

					policyMap.put("organizaname",rolegroupmap.get("ROLEGROUPDESCRIPTION"));
					policyMap.put("organiza",rolegroupmap.get("ORG_ID"))
					
					
					
					policyMap.put("carno","");
					policyMap.put("b_plate","");
					policyMap.put("business_source","");
					policyMap.put("procuration","");
					policyMap.put("frameno","");
					policyMap.put("engineno","");
					policyMap.put("brandname","");
					policyMap.put("coverage_name","");
					policyMap.put("carage","");
					policyMap.put("seatnum","0");
					policyMap.put("renewinsuranceflag","0");
					policyMap.put("isnwemployee","");
					policyMap.put("coverage_name","");
					
				
					policyMap.put("errorcode","1");
					policyMap.put("errormsg","成功");
					return policyMap;
				}
			}
            

        }


        //调用案件信息接口的方法
        public  Map CaseContent(String caseno,EliteBean eliteBean){
            HashMap<String,Object> caseMap=new HashMap<String,Object>();
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

            //获取url地址
            def urllist=eliteBean.chaxun_sql("select url from sysc_interface where urltype='caseInformationr'","dataSource");
            def urlmap=urllist[0];
            String url=urlmap.get("url");
            log.debug("url:"+url);

            String Xmlbody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> \r\n" +
                    "<SOAP-ENV:Body> \r\n" +
                    "<m:founderQueryCaseForXml xmlns:m=\"http://service.interf.claimcar.sino.ins/\"> \r\n" +
                    "<arg0><![CDATA[<?xml version=\"1.0\" encoding=\"GBK\"?> \r\n" +
                    "<PACKET> \r\n" +
                    "  <HEAD></HEAD>  \r\n" +
                    "  <BODY>\r\n" +
                    "     <RegistNo>"+caseno+"</RegistNo>\r\n" +
                    "  </BODY> \r\n" +
                    "</PACKET> \r\n" +
                    "]]> \r\n" +
                    "</arg0> \r\n" +
                    "</m:founderQueryCaseForXml> \r\n" +
                    "</SOAP-ENV:Body> \r\n" +
                    "</SOAP-ENV:Envelope>";

            Map headMap=new HashMap(); //设置头文件
            headMap.put("Content-Type", "text/xml");
            def httpresult=Util.doHttpPostUseHead(url,Xmlbody,casecharset,timeout,caseenableSSLflag,headMap);
            log.debug("httpresult:"+httpresult);
            log.debug("Xmlbody:"+Xmlbody);

            org.json.JSONObject caseJson=XML.toJSONObject(httpresult);
            log.debug("caseJson转换成Json后的报文:"+caseJson);

            org.json.JSONObject caseJson2=XML.toJSONObject(caseJson.getJSONObject("S:Envelope").getJSONObject("S:Body").getJSONObject("ns2:founderQueryCaseForXmlResponse").getString("return"));
            log.debug("caseJson2:"+caseJson2);
            log.debug("NodeInfoList:"+caseJson2.toString().indexOf("NodeInfo\""));

            if(caseJson2.getJSONObject("PACKET").getJSONObject("HEAD").get("ResponseType") !=1){
                caseMap.put("errorcode","-1");
                caseMap.put("errormsg","未返回案件信息");
                return caseMap;
            }else if (caseJson2.toString().indexOf("NodeInfo\"")<0){
                caseMap.put("errorcode","-1");
                caseMap.put("errormsg","未返回案件信息");
                return caseMap;
            }else{
                org.json.JSONArray caseList=caseJson2.getJSONObject("PACKET").getJSONObject("BODY").getJSONObject("NodeInfoList").getJSONArray("NodeInfo");
                log.debug("caseList案件接口返回的正文:"+caseList);
                def caseListSize = caseList.length();
                log.debug("caseListSize长寸:"+caseListSize);
                def TASKTIME,NODE;
                List<String> arr=new ArrayList<>();
                for(int i=0;i<caseListSize;i++){
                    if(i==0){
                        TASKTIME=caseList[i].get("TaskInTime");
                        NODE=caseList[i].get("TaskName");
                        log.debug(TASKTIME+'   '+NODE);
                    }
                    if(caseList[i].get("NodeCode")=="Regis"){
                        log.debug("caseList["+i+"]"+caseList[i]);
                        caseMap.put("report_time",caseList[i].get("ReportTime"));
                    }
                    if(i>0&&TASKTIME<caseList[i].get("TaskInTime")){
                        TASKTIME=caseList[i].get("TaskInTime");
                        NODE=caseList[i].get("TaskName");
                        log.debug(TASKTIME+'   '+NODE);
                    }
                    if(caseList[i].get("NodeCode")=='Check'){
                        if(caseList[i].toString().indexOf("HandlerUser")>0) {
                            caseMap.put("examinename", caseList[i].get("HandlerUser"));
                        }else{
                            caseMap.put("examinename", "");
                        }
                        if(caseList[i].toString().indexOf("HandlerType")>0) {
                            caseMap.put("examine_type",caseList[i].get("HandlerType"));
                        }else{
                            caseMap.put("examine_type","");
                        }
                    }
                    if(caseList[i].get("NodeCode")=='DLoss'&&caseList[i].toString().indexOf("RepairFactoryName")>0 && caseList[i].get("RepairFactoryName") ){
                        caseMap.put("repairfactoryname",caseList[i].get("RepairFactoryName"));
                        if(caseList[i].toString().indexOf("HandlerUser")>0) {
                            arr.add(caseList[i].get("HandlerUser"));
                        }
                        if(caseList[i].toString().indexOf("HandlerType")>0) {
                            caseMap.put("loss_type",caseList[i].get("HandlerType"));
                        }else{
                            caseMap.put("loss_type","");
                        }

                    }
                }

                def qa="";
                for(int j=0;j<arr.size();j++){
                    qa=qa+arr.get(j)+',';
                    if(j==0){
                        log.debug(arr.get(j));
                        caseMap.put("losser",arr.get(j));

                    }
                }
                caseMap.put("losser_list",qa.substring(0,qa.size()-1));

                if(TASKTIME||NODE){
                    caseMap.put("arrive_time",TASKTIME);
                    caseMap.put("report_nodecode",NODE);
                }


                caseMap.put("errorcode","1");
                caseMap.put("errormsg","成功");
                return caseMap;
            }
        }

    }
